#' ---
#' title: "Revised summary stats table"
#' author: ""
#' date: "`r Sys.Date()`"
#' output: pdf_document
#' header-includes:
#'  - \usepackage{booktabs}
#'  - \usepackage{longtable}
#'  - \usepackage{array}
#'  - \usepackage{multirow}
#'  - \usepackage{wrapfig}
#'  - \usepackage{float}
#'  - \usepackage{colortbl}
#'  - \usepackage{pdflscape}
#'  - \usepackage{tabu}
#'  - \usepackage{threeparttable}
#'  - \usepackage{threeparttablex}
#'  - \usepackage[normalem]{ulem}
#'  - \usepackage{makecell}
#'  - \usepackage{dcolumn}
#'  - \usepackage{setspace}\doublespacing
#' ---


## ---- spin_code, eval = FALSE, include = FALSE ----
# spin code to output Rmd
# set knit = FALSE to generate Rmd / Rnw but not compile
# rmarkdown::render(input = here::here("code/summary_stats_revised.R"), output_format = "pdf_document", clean = TRUE)

# running rmarkdown::render generates summary_stats_revised.tex
# last bit of code extracts middle rows of summary stats tables
# so that N row can be customized in main doc
# 


## ---- setup, eval = TRUE, include = FALSE ----
library(knitr)
library(here) 
opts_chunk$set(fig.lp = "fig:", echo = FALSE, message = FALSE, warning = FALSE, error = FALSE)
options(scipen = 10)

## ---- load_packages1 ----
library(tidyverse)
library(xtable)
library(kableExtra)
library(janitor)
#devtools::install_github("hughjonesd/santoku")
library(santoku)

# format for tables
star_format <- case_when(
    knitr::is_latex_output() ~ "latex",
    knitr::is_html_output()  ~ "html",
    TRUE                     ~ "text"
)

kable_format <- case_when(
    knitr::is_latex_output() ~ "latex",
    knitr::is_html_output()  ~ "html",
    TRUE                     ~ "markdown"
)

## ---- load_data ----
load(file = here("data/summary_stats.Rdata"), verbose = TRUE)
load(file = here("data/summary_stats_unmatched.Rdata"), verbose = TRUE)

# south_row <- which(row.names(summary_nv64) == "% South")
# mean_cols <- which(str_detect(col.names(summary_nv64), "Mean"))

# summary_nv64[south_row,  ] 
#save(summary_nv64, summary_v68, summary_v268, file = here("data/summary_stats.Rdata"))


## ---- view_data, eval = FALSE ----
# View(summary_nv64)
# View(summary_v68)
# View(summary_v268)




## ---- combine_summary_stats ----

summary_stats_matched <- cbind(summary_nv64, summary_v68)
summary_stats_matched <- cbind(summary_stats_matched, summary_v268)

row_names <- row.names(summary_stats_matched)

summary_stats_unmatched <- cbind(summary_nv64_unmatched, summary_v68_unmatched)
summary_stats_unmatched <- cbind(summary_stats_unmatched, summary_v268_unmatched)

#View(summary_stats_matched)
#View(summary_stats_unmatched)


# make single digit but hold lagging zeros
summary_stats_matched <-
    apply(
        X      = summary_stats_matched,
        MARGIN = 2,
        FUN    = function(x) {
            format(x, digits = 1, nsmall = 1)
            # formatC(x, format='f', digits = 1) 
        }
    )

# needed to round first
summary_stats_unmatched <-
  apply(
    X      = summary_stats_unmatched,# %>% as.matrix() ,
    MARGIN = 2,
    FUN    = function(x) {
      round(x, 1) %>% 
      format(digits = 1, nsmall = 1) 
      # formatC(x, format='f', digits = 1) 
    }
  )

#row.names(summary_stats_matched) <- row_names





# create index of sd columns
sd_cols <- seq(from = 2, to = 12, by = 2)

# add parentheses to sd cols
summary_stats_matched[ , sd_cols] <-
    apply(
        X      = summary_stats_matched[ , sd_cols],
        MARGIN = 2,
        FUN    = function(x) {
            str_trim(x, side = "both") %>% 
            paste0("(", ., ")") 
        }
    )

summary_stats_unmatched[ , sd_cols] <-
  apply(
    X      = summary_stats_unmatched[ , sd_cols],
    MARGIN = 2,
    FUN    = function(x) {
      str_trim(x, side = "both") %>% 
        paste0("(", ., ")") 
    }
  )

summary_stats_df <- summary_stats_matched %>% 
    as.data.frame() %>% 
    rownames_to_column() 
    
summary_stats_df_unmatched <- summary_stats_unmatched %>% 
  as.data.frame() %>% 
  rownames_to_column() 


## ---- xtable_print_summary_stats, results = "asis", eval = FALSE ----

xtable(summary_stats_df)

xtable(summary_stats_df_unmatched)

## ---- kable_print_summary_stats, results = "asis" ----

kable_out_matched <- summary_stats_df %>%
  kable(
    format    = kable_format,
    booktabs  = TRUE,
    align     = c("l", rep("r", 12)),
    linesep   = c('', '', '', '', '', '', '', '', '', '', '', '', '\\addlinespace'),
    col.names = c("", rep(c("Mean", "(SD)"), 6))
  ) %>%
  kable_styling(latex_options = c("hold_position", "scale_down")) %>%
  
    # specifying column widths broke alignment
    # https://github.com/haozhu233/kableExtra/issues/340
    # couldn't get workaround to work
    
    #column_spec(2, width = ".75cm") %>% 
    #column_spec(seq(from = 3, to = 13, by = 2), width = ".5cm") %>% 
    #column_spec(3, width = ".5cm") %>% 
    #column_spec(5, width = ".5cm") %>% 
    #column_spec(7, width = ".5cm") %>% 
    add_header_above(c(
            " ",
            "Controls" = 2,
            "Treated"  = 2,
            "Controls" = 2,
            "Treated"  = 2,
            "Controls" = 2,
            "Treated"  = 2
        )) %>% 
    add_header_above(c(
        " ",
        "DCA (nonviolent)" = 4,
        "DCA (violent)"    = 4,
        "Carter (violent)" = 4
    ))

kable_out_matched

cat(kable_out_matched, 
    file = here("code/summary_stats_revised_extract_matched_full_table.tex"))


kable_out_unmatched <- summary_stats_df_unmatched %>%
  kable(
    format    = kable_format,
    booktabs  = TRUE,
    align     = c("l", rep("r", 12)),
    linesep   = c('', '', '', '', '', '', '', '', '', '', '', '', '\\addlinespace'),
    col.names = c("", rep(c("Mean", "(SD)"), 6))
  ) %>%
  kable_styling(latex_options = c("hold_position", "scale_down")) %>%
  
  # specifying column widths broke alignment
  # https://github.com/haozhu233/kableExtra/issues/340
  # couldn't get workaround to work
  
  #column_spec(2, width = ".75cm") %>% 
  #column_spec(seq(from = 3, to = 13, by = 2), width = ".5cm") %>% 
  #column_spec(3, width = ".5cm") %>% 
  #column_spec(5, width = ".5cm") %>% 
  #column_spec(7, width = ".5cm") %>% 
  add_header_above(c(
    " ",
    "Controls" = 2,
    "Treated"  = 2,
    "Controls" = 2,
    "Treated"  = 2,
    "Controls" = 2,
    "Treated"  = 2
  )) %>% 
  add_header_above(c(
    " ",
    "DCA (nonviolent)" = 4,
    "DCA (violent)"    = 4,
    "Carter (violent)" = 4
  ))

kable_out_unmatched

cat(kable_out_unmatched, 
    file = here("code/summary_stats_revised_extract_unmatched_full_table.tex"))

## ---- other_tables_check, results = "asis" ----

summary_nv64 %>% 
    kable(
      format = kable_format,
      booktabs = TRUE) %>% 
    kable_styling(latex_options = c("hold_position", "scale_down"))
                  
    
summary_v68 %>% 
    kable(
        format = kable_format,
        booktabs = TRUE) %>% 
    kable_styling(latex_options = c("hold_position", "scale_down"))
    

summary_v268 %>% 
    kable(
        format = kable_format,
        booktabs = TRUE) %>% 
    kable_styling(latex_options = c("hold_position", "scale_down"))


## ---- save_summary_stat_extract, eval = FALSE ----

# Run first time with  eval = FALSE
# Run second time with eval = TRUE
# Second run extracts two summary stats tables to \input to Rnw:
#   summary_stats_revised_extract_matched.tex
#   summary_stats_revised_extract_unmatched.tex

## MATCHED
# read in table output    
x <- readLines(here("code/summary_stats_revised.tex"))

# identify start and end of main results of matched data
start <- which(str_detect(x, "\\% Black"))[1]
end   <- which(str_detect(x, "log\\(Population\\)"))[1]

# extract relevant lines
y     <- x[start:end] 

# write extracted lines to new .tex file for input to main doc
cat(y, file=here("code/summary_stats_revised_extract_matched.tex"), sep="\n" )



## UNMATCHED
# identify start and end of main results of unmatched data
start <- which(str_detect(x, "\\% Black"))[2]
end   <- which(str_detect(x, "log\\(Population\\)"))[2]

# extract relevant lines
y     <- x[start:end] 

# write extracted lines to new .tex file for input to main doc
cat(y, file=here("code/summary_stats_revised_extract_unmatched.tex"), sep="\n" )


